package repeat2;

import jdk.internal.util.xml.impl.Input;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2024-10-12
 * Time: 9:57
 */
public class TcpEchoClient {
    private Socket socket = null;
    public TcpEchoClient(String serverIp , int serverport){
        socket = new Socket();

    }
    public void start(){
        System.out.println("客户端启动");
        //从读取控制台的请求
        //构造请求并发送
        //接收响应
        //把相应打印显示出来
        try(InputStream inputStream = socket.getInputStream();
            OutputStream outputStream = socket.getOutputStream()){
            Scanner scannerConsole = new Scanner(System.in);
            Scanner scannerNetWork = new Scanner(inputStream);
            PrintWriter writer = new PrintWriter(outputStream);
            while(true){
                System.out.print("->");
                String request = scannerConsole.next();
                //怎么发送呢？outputStream(带封装)
                writer.println(request);
                //怎么接收响应?
                String response = scannerNetWork.next();
                System.out.println(response);


            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public static void main(String[] args) throws IOException {
        InternetTcp.TcpEchoClient client = new InternetTcp.TcpEchoClient("127.0.0.1",9091);
        client.start();

    }
}
